# -*- conding:utf-8 -*-

from algorithm.newConstrainsAlgorithm import ConstrainsAlgorithm
from algorithm.newNoConstrainsAlgorithm import NoConstrainsAlgorithm
from algorithm.randomGraphRequest import randomGR
import json

if __name__ == '__main__':
    # N为结点数目，应由程序传入
    N = 50
    # function_V_num为服务器结点,默认值即可
    function_V_num = (int)(N / 10)
    # p为结点间连线的概率，默认值即可
    p = 3 / N
    # max_E_cost为最大的边权值,默认值即可
    max_E_cost = 10
    # 功能数,就默认值即可
    function_num = 5
    # 最大包大小，默认值即可
    max_bk = 5
    # 最大功能消耗，默认值
    max_deployment_cost = 5
    # 并行功能数，默认值
    parallel_num = 3
    # 目的结点占比，默认值
    destination_V_radio = 0.1
    # 并行功能起始位置，默认0
    parallel_position = 0
    # -----------------上面是一些默认的参数信息----------------------------#
    rGR = randomGR()
    """
    返回值介绍
    initial_Graph是一个MGraph类的实例，里面的图矩阵和结点就是我们要显示的拓扑图数据结构。边为inf的就是无连边权值无限大，在图中inf的边不展示
    function_V是服务器结点，在拓扑图中这种结点可以用其他颜色表示。如果太麻烦就算了
    E_constrains, function_V_constrains 是边和结点资源限制，不用展示。但在计算算法时要用到
    neibor_function_v 算法计算要用到，不用展示
    """
    initial_Graph, function_V, E_constrains, function_V_constrains, neibor_function_v = rGR.random_initial_Graph(N,
                                                                                                                 function_V_num,
                                                                                                                 p,
                                                                                                                 max_E_cost)
    # 初始化图，
    graph = {'graph_V': initial_Graph.V, 'graph_E': initial_Graph.E.tolist()}
    request = rGR.random_request(N, function_num, function_V_num, max_bk, max_deployment_cost, parallel_num, function_V,
                                 destination_V_radio, neibor_function_v, initial_Graph, parallel_position)
    deployment_cost = request['deployment_cost']
    deployment_cost = deployment_cost.tolist()
    request['deployment_cost'] = deployment_cost

    # 两个展示的。一个是图一个是请求
    graph_json = json.dumps(graph)
    request_json = json.dumps(request)

    """
    算法1,传入参数是前面生成的请求和网络图
    返回结果中，Tmatrix和Ttable的树，matrix是邻接矩阵，table是邻接表。计算结果展示上可以在原来的拓扑图上描边。
    太麻烦的话就返回weightsum和SFC，weightsum是计算结果的消耗，SFC是服务功能链的顺序
    SFC: {'weight': 565.0, 'route': [36, 0, 17, 17, 17, 0, 11, 34, 34], 'deploy_Node': [17, 17, 17, 34, 34], 'deploy_server': [0, 1, 2, 3, 4]}
    SFC中的weight是功能链的代价，route是功能部署的路径，deploy_Node时功能部署的结点，deploy_server是部署的功能
    """
    """
    树和路径考虑用其他颜色的线展示在结果中，如果太麻烦就算了。直接表格展示就行
    """
    NCA = NoConstrainsAlgorithm()
    Tmatrix, Ttable, weightsum, SFC, T = NCA.noContraintTackleRequest(request, parallel_num, initial_Graph, function_V)
    print(weightsum)
    print(SFC)

    """weightsum为总权值，resultTree就是计算出来的树，deployNode是功能部署位置（对应服务器结点），deployServer对应部署的功能"""
    NCA_result = {'weightsum': weightsum, 'resultTree': T.tolist(), 'deployNode': SFC['deploy_Node'],
                  'deployServer': SFC['deploy_server']}
    NCA_result_json = json.dumps(NCA_result)
    """
    算法2,用法和需求跟算法1一样
    """
    CA = ConstrainsAlgorithm()
    Tmatrix, Ttable, weightsum, SFC, T = CA.ContraintTackleRequest(request, initial_Graph, function_V, E_constrains,
                                                                   function_V_constrains)
    print(weightsum)
    print(SFC)
    CA_result = {'weightsum': weightsum, 'resultTree': T.tolist(), 'deployNode': SFC['deploy_Node'],
                 'deployServer': SFC['deploy_server']}
    CA_result_json = json.dumps(CA_result)
    print(CA_result_json)
    # See PyCharm help at https://www.jetbrains.com/help/pycharm/
